// JavaScript Document

/**
 * import compontent 
 */
Vsg.importClass(
	[
		"Vsg.DataSourceEntity.DataSourceEntityBuilder",
		"Vsg.EntityRecord.FinaceFlowing",
		"Vsg.Component.AStockFinaceFlowingPanel"	
	]
);

function initFlashComponent() {
	try {
		var so = new SWFObject("../../depenthird/amcharts/amstock.swf", "amstock", "100%", "100%", "10", "#ffffff");
		so.addVariable("path", "");
		so.addVariable("settings_file", encodeURIComponent("./xmlAmStockSettings.do?stockCode="+passParameters.stockCode));
		//so.addVariable("loading_settings", "正在加载图表设置");
		so.addVariable("preloader_color", "#999999");
		so.write("flashcontent");
	} catch (e) {
		alert("init flash component error : " + e);
	}
}



function getStockFinaceFlowingPanel() {
	try {
		var $dsEntity = new Vsg.DataSourceEntity.DataSourceEntityBuilder();
		$dsEntity.setUrl( UrlMKey['stk-finaceflowing-astock'] );
		$dsEntity.setEntityRecord( new Vsg.EntityRecord.FinaceFlowing() );	
		var $ds = $dsEntity.newAjaxDataSource();
		
		// --- defined the grid panel ---
		var gp = new Vsg.Component.AStockFinaceFlowingPanel({
        	store: $ds,
			frame:false,
			border:false,			
			//width:200,
        	loadMask: true,
        	viewConfig: {
            	forceFit:true
        	}						
		});
		
		// --- defined and add event ---
		gp.on("rowclick" , Vsg.getDefinedEvent("StockFinaceFlowingPanel.evt_rowclick"));
		
		return gp;	
	} catch (e) {
		alert("Get model panel error: "  );
	}	 
}

function getStockFinaceFlowingDetailForm() {
	var panel = new Ext.form.FormPanel({
    	id: "stockFinaceFlowingDetailForm",
        layout: 'form',
		autoScroll:true,		
        bodyStyle: 'padding:5px 5px 0',	
		items:[{
				xtype:'fieldset',
				title:'对冲分析(元)',
				layout: 'column',
				autoHeight:true,
				items:[{
            		columnWidth: 0.50,
            		layout: 'form',
					border:false,
            		defaults: {width: 120},
					labelWidth: 60,
					items: [{
						fieldLabel: '主力合计',
						name:'mainDiffAmount',
						xtype:'textfield',
						readOnly :'true'
					}]
        		},{
            		columnWidth: 0.50,
            		layout: 'form',
            		defaults: {width: 120},
					border:false,
					labelWidth: 60,					
            		items: [{
						fieldLabel: '散户合计',
						name:'retailDiffAmount',
						xtype:'textfield',
						readOnly :'true'
					}]
        		}]
        	},{
				xtype:'fieldset',
				title:'主力进出(元)',
				layout: 'column',
				autoHeight:true,
				items:[{
            		columnWidth: 0.50,
            		layout: 'form',
					border:false,
            		defaults: {width: 120},
					labelWidth: 60,
					items: [{
						fieldLabel: '流入资金',
						name:'mainBuyAmount',
						xtype:'textfield',
						readOnly :'true'
					},{
						fieldLabel: '买入对倒',
						name:'matchBuyAmount',
						xtype:'textfield',
						readOnly :'true'
					}]
        		},{
            		columnWidth: 0.50,
            		layout: 'form',
            		defaults: {width: 120},
					border:false,
					labelWidth: 60,					
            		items: [{
						fieldLabel: '流出资金',
						name:'mainSellAmount',
						xtype:'textfield',
						readOnly :'true'
					},{
						fieldLabel: '卖出对倒',
						name:'matchSellAmount',
						xtype:'textfield',
						readOnly :'true'
					}]
        		}]
        	}
			,{
				xtype:'fieldset',
				title:'散户进出(元)',
				layout: 'column',
				autoHeight:true,
				items:[{
            		columnWidth: 0.50,
            		layout: 'form',
					border:false,
            		defaults: {width: 120},
					labelWidth: 60,
					items: [{
						fieldLabel: '流入资金',
						name:'retailBuyAmount',
						xtype:'textfield',
						readOnly :'true'
					}]
        		},{
            		columnWidth: 0.50,
            		layout: 'form',
            		defaults: {width: 120},
					border:false,
					labelWidth: 60,					
            		items: [{
						fieldLabel: '流出资金',
						name:'retailSellAmount',
						xtype:'textfield',
						readOnly :'true'
					}]
        		}]
        	}]			
	});
	return panel;
}

/** ===============================
 * init ui interface 
 * ================================
 */
function init(){

	try {
		var title = passParameters.stockName+" [代码:"+ passParameters.stockCode +"]";
		
		var stockPanel = {
			xtype:"panel",
			id:'stock-panel',
			region:"center",
			border:false,
			title: title,
			normal: false,
			width: 400,
			html: '<div id="flashcontent"></div>'
		}
		
		var finaceFlowingPanel = {
			xtype:"panel",
			id:'finaceFlowing-panel',
			collapsible:true,			
			region:"center",
			normal: false,
			html: '<div id="finaceFlowing-flashcontent">54487</div>'			
		}
		
		// --- add top panel
		var $tabsArray = [];

		$tabsArray.push({
			title:"资金进出情况",
			items:[
				{
					region:'center',
					layout:'fit',
					border:true,
					margins:'5 5 5 5',
					items:[getStockFinaceFlowingPanel()]
				},{
					region:'east',
					layout:'fit',
					border:false,
					margins:'5 5 5 0',
					width:'450',
					items:[getStockFinaceFlowingDetailForm()]
				}
			],
			layout:"border",
			id:"tab1"
		});

		var $tabs = {
			region:"south",
			height:210,
			activeTab:0,
			frame:true,
			border:false,
			items:$tabsArray,
			plain:false
		};

		
		// --- set layout content -----
		var $layoutContent = {
			layout:"border",
			items:[
				stockPanel,
				new Ext.TabPanel($tabs)
			]
		};

    	new Ext.Viewport( $layoutContent );

		initFlashComponent();
		
		//initFinaceFlowingComponent();
	} catch (e) {
		alert('出现系统性错误，请联系管理人员!');
		//Ext.log("Initial UI error: " + e);
	}
}
// --- run the init method
Vsg.run( init );


Vsg.definedEvent("StockFinaceFlowingPanel.evt_rowclick" , function($this, rowIndex, event) {
																			   
	var formPanel = Ext.getCmp('stockFinaceFlowingDetailForm');
	
	var record = $this.getStore().getAt(rowIndex);
	
	var form = formPanel.getForm();
	
	form.loadRecord( record );
	
});